import pyautogui
import os
from pynput import keyboard

class MiddleMouseSimulator:
    def __init__(self):
        self.middle_mouse_down = False
        self.hotkey = keyboard.HotKey(
            keyboard.HotKey.parse('<ctrl>+y'),
            self.on_activate)

        self.l = keyboard.Listener(
            on_press=self.for_canonical(self.hotkey.press),
            on_release=self.for_canonical(self.hotkey.release)
        )
        self.l.start()

    def on_activate(self):
        if self.middle_mouse_down:
            pyautogui.mouseUp(button='middle')
            self.middle_mouse_down = False
            print('middle up')
        else:
            pyautogui.mouseDown(button='middle')
            self.middle_mouse_down = True
            print('middle down')

    def for_canonical(self,f):
        return lambda k: f(self.l.canonical(k))

m_simulator = MiddleMouseSimulator()